import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login'
import ww from '../views/yu'
import Home from '../views/Home'
import welcome from "@/views/welcome";
import users from "@/views/user/users"
import rights from '@/views/power/rights'
import roles from "@/views/power/roles";
import goods from "@/views/shop/goods";
import Add from "@/views/shop/Add";
import Params from "@/views/shop/Params";
import Cate from "@/views/shop/Cate";
Vue.use(VueRouter)

const routes = [
  {path:"", redirect:'/login'},
  {path:"/login", component:Login},
  {path:"/ww", component:ww},
  {path:"/home", component:Home,
    children:[
        //component表示显示的vue组件
      {path:"/home", redirect:'/welcome'},
      {path:"/welcome", component:welcome},
      { path:"/users", component: users},
      { path:"/rights", component: rights},
      { path:"/roles", component: roles},
      { path:"/goods", component: goods},
      { path: '/goods/add', component: Add },
      { path: '/params', component: Params },
      { path: '/categories', component: Cate },
    ]
  }
]
const router = new VueRouter({
  routes
})
//挂载路由导航守卫
   //router.beforeEach(function (to,from,next))
  router.beforeEach((to,from,next)=>{
//如果是登录页面直接放行

    if(to.path==='/login')return next()
    //获取token
    const tokenStr=window.sessionStorage.getItem('token')
    //判断有没有携带token，没有返回登录页面
    if(!tokenStr)return next('/login')
    next()
})
export default router
